package org.imlinker.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.imlinker.LocalVariablesManager;
import org.imlinker.account.ManageAccount;
import org.imlinker.buddy.BuddyChat;

/**
 * Show buddy info,such as username nickname...and you can edit nickname
 * displayed on your buddylist.
 *  
 */

public class BuddyInfoWindow extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The default length of JTextField.
	 */
	private final int textLength = 15;

	/**
	 * The textField used to display buddy's nickname.
	 */
	private JTextField nameField = new JTextField(textLength);

	/**
	 * The saveButton.
	 */
	private JButton saveButton = new JButton(LocalVariablesManager
			.getLocalVariables("BuddyInfoWindow.SaveButton"));

	/**
	 * The chatCell.
	 */
	private BuddyChat buddyChat;

	/**
	 * The constructor.
	 * 
	 * @param paraBuddyChat
	 *            The buddy's chatCell
	 */
	public BuddyInfoWindow(final BuddyChat paraBuddyChat) {
		super(LocalVariablesManager.getLocalVariables("BuddyInfoWindow"));
		setName("BuddyInfoWindow");
		setIconImage(ManageAccount.getLogo());

		this.buddyChat = paraBuddyChat;

		Container container = getContentPane();

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0, 2));

		// Add localName label.
		panel.add(new JLabel(LocalVariablesManager
				.getLocalVariables("BuddyInfoWindow.UserEmailLabel")));
		JTextField userEmail = new JTextField(buddyChat.getUser());
		userEmail.setEditable(false);
		panel.add(userEmail);
		panel.add(new JLabel(LocalVariablesManager
				.getLocalVariables("BuddyInfoWindow.UserStatusLabel")));
		JTextField userStatus;

		if (buddyChat.getStatus() != "") {
			userStatus = new JTextField(buddyChat.getStatus().substring(
					buddyChat.getStatus().indexOf("(") + 1,
					buddyChat.getStatus().lastIndexOf(")")));
		} else {
			userStatus = new JTextField("");
		}
		userStatus.setEditable(false);
		panel.add(userStatus);
		panel.add(new JLabel(LocalVariablesManager
				.getLocalVariables("BuddyInfoWindow.LocalNameLabel")));
		nameField.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				changeName();
			}

		});
		nameField.setText(buddyChat.getName());
		panel.add(nameField);

		// Add saveButton to the last row.
		panel.add(new JLabel(""));
		saveButton.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				changeName();
			}

		});
		panel.add(saveButton);
		container.add(panel, BorderLayout.NORTH);

		// Set location.
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * Change buddy's nickname.
	 * 
	 */
	protected final void changeName() {
		String name = nameField.getText();

		buddyChat.setBuddyName(name);
		this.dispose();
	}

}
